﻿using KingApp.DAL;
using KingApp.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KingApp.DB
{
    public sealed class LotteryData
    {
        private static LotteryData instance;
        private static object syncLock = new object();
        private List<Lottery> lotterys;

        private List<Lottery> testLotterys;

        private LotteryData()
        {
            var loDatas = XmlHelper.GetXmlItems();
            var index = 1;
            lotterys = loDatas.Select(d => new Lottery
            {
                Id = index++,
                Issue = d.Attribute("issue").Value,
                LotteryNumber = d.Attribute("lottery").Value,
                Time = d.Attribute("time").Value

            }).ToList();

            var count = this.lotterys.Count();
            for (int i = 0; i < count - 2; i++)
            {
                // this.lotterys[i].LastIssue = this.lotterys[i + 1];
            }

            //this.testLotterys = loDatas.Select(d => new LotteryModel
            //{
            //    Id = index--,
            //    Issue = d.Attribute("issue").Value,
            //    WinningNumber = d.Attribute("lottery").Value,
            //    ModifiedTime = d.Attribute("time").Value

            //}).ToList();
        }

        /// <summary>
        ///  验证 数据
        /// </summary>
        public List<Lottery> TestLotterys
        {
            get
            {
                return this.testLotterys;
            }
            set
            {
                this.testLotterys = value;
            }
        }

        public List<Lottery> Lotterys
        {
            get
            {
                return this.lotterys;
            }
        }

        public static LotteryData GetInstance()
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new LotteryData();
                    }
                }
            }

            return instance;
        }

        public static void ClearInstance()
        {
            instance = null;
        }

    }
}
